package backend;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;

public class Security {
	
	private Security(){
		
	}
	
	/**
	 * Retorna el token de autenticaci�n en caso de q haya sido satisfactorio el logeo.
	 * @return
	 */
	public static String[] signIn(String username, String password) throws Exception{
		
		Connection.setServiceUrl("http://eiffel.itba.edu.ar/hci/service/Security.groovy");
		
		String [] resp=new String[5];
		
		String[] args={"username="+username,"password="+password};
		
		
		Document dom = Connection.connect("SignIn", "GET", args);
		
		String token=dom.getElementsByTagName("token").item(0).getTextContent();
		
		NamedNodeMap info=dom.getElementsByTagName("user").item(0).getAttributes();
		
		String id=info.getNamedItem("id").getNodeValue();
		String uname=info.getNamedItem("username").getNodeValue();
		String name=info.getNamedItem("name").getNodeValue();
		String last_login=info.getNamedItem("last_login_date").getNodeValue();
		
		resp[0]=token;
		resp[1]=id;
		resp[2]=uname;
		resp[3]=name;
		resp[4]=last_login;
				
		return resp;
	}
	
	

	public static void signOut(String username, String token) throws Exception{
		
		Connection.setServiceUrl("http://eiffel.itba.edu.ar/hci/service/Security.groovy");
				
		String[] args={"username="+username,"authentication_token="+token};
		
		Connection.connect("SignOut", "GET", args);
		
	}	
	

	public static void changePassword(String username, String password,String new_password) throws Exception{
	
		Connection.setServiceUrl("http://eiffel.itba.edu.ar/hci/service/Security.groovy");
		
		
		String[] args={"username="+username,"password="+password,"new_password="+new_password};
		
		Connection.connect("ChangePassword", "GET", args);
	}	

	public static void createAccount(String username,String name,String password,String email,String birth_date) throws Exception{
	
		Connection.setServiceUrl("http://eiffel.itba.edu.ar/hci/service/Security.groovy");
			
		String[] args={"account=<account><username>"+username+"</username>"+
								"<name>"+name+"</name>" +
								"<password>"+password+"</password>"+
								"<email>"+email+"</email>"+
								"<birth_date>"+birth_date+"</birth_date></account>"};
		
		Connection.connect("CreateAccount", "POST", args);
	}	

	public static String[] getAccount(String username, String token) throws Exception{
		
		Connection.setServiceUrl("http://eiffel.itba.edu.ar/hci/service/Security.groovy");
		
		String [] resp=new String[4];
		
		String[] args={"username="+username,"authentication_token="+token};
		
		Document dom = Connection.connect("GetAccount", "GET", args);
		
		String uname=dom.getElementsByTagName("username").item(0).getTextContent();
		String name=dom.getElementsByTagName("name").item(0).getTextContent();
		String email=dom.getElementsByTagName("email").item(0).getTextContent();
		String birth_date=dom.getElementsByTagName("birth_date").item(0).getTextContent();
				
		resp[0]=uname;
		resp[1]=name;
		resp[2]=email;
		resp[3]=birth_date;
				
		return resp;
	}


	public static void updateAccount(String username,String token,String name,String email,String birth_date) throws Exception{
		
		Connection.setServiceUrl("http://eiffel.itba.edu.ar/hci/service/Security.groovy");
			
		String[] args={"username="+username,"authentication_token="+token,
					   "account=<account><name>"+name+"</name>"+
								"<email>"+email+"</email>"+
								"<birth_date>"+birth_date+"</birth_date></account>"};
		
		Connection.connect("UpdateAccount", "POST", args);
	}	












	
	
}